package org.jeecg.modules.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.request.OapiProcessinstanceCreateRequest;
import com.dingtalk.api.response.OapiProcessinstanceCreateResponse;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.config.Constant;
import org.jeecg.modules.config.URLConstant;
import org.jeecg.modules.model.ProcessInstanceInputVO;
import org.jeecg.modules.service.CallbackService;
import org.jeecg.modules.util.AccessTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

/**
 * @ClassName:ProfessorModiy
 * @Description: TODO
 * @author: wang yue niao
 * @date 2020/9/8 14:57
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/org/diyProfessorInfo")
public class ProfessorModiy {
    @Autowired
    private CallbackService callbackService;
    /**
     * 专家申请修改个人信息
     */
    @PostMapping("/modeify")
    public Result<?> Modify(@RequestBody JSONObject jsonObject, HttpServletRequest request){
        Result result=new Result();
        String modaltext = jsonObject.getString("ModalText");
        Subject subject = SecurityUtils.getSubject();
        LoginUser user = (LoginUser) subject.getPrincipal();

        //获取token
        String token = request.getHeader("X-Access-Token");
        //获取当前登录用户的unionid username就是unionid
        String username = JwtUtil.getUsername(token);
        // 根据unionid获取userid
        String useridStr = callbackService.selUserIdByUnionid(username);
        JSONObject getUserId = JSON.parseObject(useridStr);
        String userid = getUserId.getString("userid");

        //发送到钉钉上进行审批
        DefaultDingTalkClient client = new DefaultDingTalkClient(URLConstant.URL_PROCESSINSTANCE_START);
        OapiProcessinstanceCreateRequest request1 = new OapiProcessinstanceCreateRequest();
        request1.setProcessCode(Constant.MODIFY_PROCESS_CODE);

        ProcessInstanceInputVO processInstance = new ProcessInstanceInputVO();
        processInstance.setOriginatorUserId(userid);
        processInstance.setDeptId(1L);
        ProcessInstanceInputVO.TextForm textForms = new ProcessInstanceInputVO.TextForm();
        textForms.setName("原因");
        textForms.setValue(modaltext);
        ArrayList<ProcessInstanceInputVO.TextForm> textForms1 = new ArrayList<>();
        textForms1.add(textForms);
        processInstance.setTextForms(textForms1);
        request1.setFormComponentValues(processInstance.generateForms());

        request1.setOriginatorUserId(processInstance.getOriginatorUserId());
        request1.setDeptId(processInstance.getDeptId());
        OapiProcessinstanceCreateResponse response = null;
        try {
            response = client.execute(request1, AccessTokenUtil.getToken());
            System.out.println(response);
            result.setSuccess(true);
        } catch (ApiException e) {
            result.setSuccess(false);
            e.printStackTrace();
        }
        return result;
    }
}
